import java.awt.Color;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.List;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Device;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.RowData;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;


import com.textdisguise.www.TextDisguise.CaptchaService.CaptchaImageWordModel;
import com.textdisguise.www.TextDisguise.CaptchaService.CaptchaService;
import com.textdisguise.www.TextDisguise.CaptchaService.CaptchaServiceLocator;
import com.textdisguise.www.TextDisguise.CaptchaService.CaptchaServiceSoapProxy;
import com.textdisguise.www.TextDisguise.CaptchaService.ClientAuthModel;


/**
 *  Using Captcha web service
 *
 * @author Emad, Hoang
 *         Created May 10, 2012.
 */

public class Captcha {
	
	//==============================================
	private static String clientGuid = "64fc53cd-f471-4ca6-82bb-a51eb8a6bf21";
	private static String clientPassword = "`QX9f:v6!#OA)Ug|mO,/8A%%xy";
	private static String Image_giud;
	private static String Image_url;
	private static Label lbMessage;
	private static Text textbox;
	//==============================================
	
	

	/**
	 * .
	 *
	 * @param args
	 */
	public static void main(String[] args) {
		
		//retrieve captcha guid and image url form the web service
		LoadCaptcha();
		System.out.println(Image_giud);
		System.out.println(Image_url);
		
		//=====
		//SWT
		//=====
		Display display = new Display();
		Shell shell = new Shell(display);
		shell.setLayout(new RowLayout());
		shell.setText("Captcha Application");
		
		//layout
		Composite parent = new Composite(shell, SWT.NONE);
		RowLayout rowLayout = new RowLayout(SWT.VERTICAL);
		parent.setLayout(rowLayout);
	
		//image
		URL url;
		try {
			//Loading Image
			url = new URL(Image_url);
			Label label = new Label(parent,SWT.NONE);
			Image image;
			image = new Image(display,url.openStream());
			label.setImage(image);
			
			//Textbox
			textbox = new Text(parent, SWT.NONE);
			textbox.setSize(2,textbox.getSize().y);
			RowData rowData = new RowData();
			textbox.setLayoutData(rowData);
			
			//Button
			Button button = new Button(parent, SWT.PUSH);
			rowData = new RowData();
			button.setText("Validate");
			button.setLayoutData(rowData);
			button.addSelectionListener(new SelectionAdapter() {
				
				public void widgetSelected(SelectionEvent e) {
					System.out.println("Captcha Validating...");
					
					//Captcha Service calls for validating
					ClientAuthModel _AuthModel;
					CaptchaImageWordModel _WordModel;
					CaptchaServiceSoapProxy _proxy;
					
					_AuthModel = new ClientAuthModel(clientGuid,clientPassword);
					_proxy = new CaptchaServiceSoapProxy();
					
					boolean bool = false;
					try {
						bool = _proxy.validateImageWord(_AuthModel,Image_giud,textbox.getText());
						if(bool)
						{
							lbMessage.setText("Validated!");
							System.out.println("Validated!.");
						}
						else
						{
							lbMessage.setText("Flase, try again");
							System.out.println("Flase, try again");
						}
						lbMessage.pack(true);
					} catch (RemoteException exception) {
						exception.printStackTrace();
					}
					
					
				}
			});
			
			//Label
			lbMessage = new Label(parent,SWT.NONE);
			rowData = new RowData();
			lbMessage.setLayoutData(rowData);
			
		} catch (IOException exception) {
			exception.printStackTrace();
		}
		
		
		
		//open the window
		shell.pack();
		shell.open();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
		display.dispose();
		
		
	}
	
	/**
	 *  Retrive image url and giud 
	 *  from the Captcha Web service 
	 */
	public static void LoadCaptcha()
	{
		ClientAuthModel _AuthModel;
		CaptchaImageWordModel _WordModel;
		CaptchaServiceSoapProxy _proxy;
		String giud;
		String url;
		
		try {
			//Authentication
			_AuthModel = new ClientAuthModel(clientGuid,clientPassword);
			_proxy = new CaptchaServiceSoapProxy();
			
			_WordModel = _proxy.getNewWord(_AuthModel);
			Image_giud = _WordModel.getCaptchaImageGuid();
			Image_url = _WordModel.getUrl();
			
		} catch (RemoteException exception) {
			exception.printStackTrace();
		}
		


		//ViewState.Add("CaptchaImageGuid", captchaImage_WordModel.CaptchaImageGuid);

		//guid.Text = captchaImage_WordModel.CaptchaImageGuid;
		//img.Text = "<img src=\"" + captchaImage_WordModel.Url + "\" border=\"1\" alt=\"CAPTCHA-image\">";

		
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
}
